<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
cfc('core')->checklogin();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
    $fields=array('id','ordersn','name','mobile','cel_fee','driver_id','mid','createtime','status','price','price1','price2','backfee','waitfee','start_seat','dest_seat','juli','cuid');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_daijia_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	if($return['list']){
		foreach($return['list'] as &$v){
			if($v['cuid'] && $v['cuid']!=$_SESSION['uid']){
				$v['cel']=1;
			}
			$v['cel_tip']=$this->model('order')->cel_check($v,true);
			$v['driver']=pdo_get('jy_daijia_driver',array('id'=>$v['driver_id']),array('name','mobile','sex','avatar','mid'));
			$mobile=$v['driver']['mobile'];
			if($return['pluginset']['open_ysmobile']){
				$ysmobile=$this->plugin('jy_alisms')->bindx($v['mobile'],$mobile);
				if($ysmobile['type']){
					$v['driver']['ysmobile']='error隐藏电话失败：'.$ysmobile['message'];
				}else{
					$v['driver']['ysmobile']=$ysmobile;
				}
			}else{
				$v['driver']['ysmobile']=$v['driver']['mobile'];
			}
			if($mobile){
				$sub1=substr($mobile,0,3);
				$sub2=substr($mobile,-4);
				$v['driver']['mobile']=$sub1.'****'.$sub2;
			}
			$v['driver']['ordersum']=pdo_count('jy_daijia_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'driver_id'=>$v['driver_id']),'id');
			$v['driver']['avatar']=tomedia($v['driver']['avatar']);
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		}
	}
	$return['type']=$this->order_status;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_daijia_order',array('id'=>$id));
		pdo_delete('jy_daijia_order',array('id'=>$id));	
	}
	exi('删除成功！','','jy_daijia/order/list/');

}
if($_RQ['op']=='getcel'){
	$id=intval($_RQ['id']);	
	if($id){
		$order=pdo_get('jy_daijia_order',array('id'=>$id));	
		if($order['status']==1){
			$res['tip']=$this->model('order')->cel_check($order,true);
		}
	}
	exi($res);
}
if($_RQ['op']=='cel'){
	$id=intval($_RQ['id']);	
	if($id){
		$order=pdo_get('jy_daijia_order',array('id'=>$id));	
		if($order['status']==1){
			$cel_fee=$this->model('order')->cel_upfee($order);
			pdo_update('jy_daijia_order',array('cel_fee'=>$cel_fee,'status'=>2,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}else{
			pdo_update('jy_daijia_order',array('status'=>3,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}
	}
	exi('已申请取消,请联系对方确认','','jy_daijia/order/list/');
}
if($_RQ['op']=='dcel'){
	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_cancel($id);
	}
	exi('已确定取消','','jy_daijia/order/list/');
}
if($_RQ['op']=='call'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_daijia_order',array('id'=>$id));
	pdo_update('jy_daijia_order',array('createtime'=>TIMESTAMP),array('id'=>$id));
	$this->model('worker')->syspai($order);
	$params=array(
			'mid'=>$order['mid'],
			'message'=>'您重新呼叫了一次代驾！',
	);
	$this->model('notice')->add_msg($params);
	exi('呼叫成功！');
}
if($_RQ['op']=='done'){
	$id=intval($_RQ['id']);	
	if($id){
		$order=pdo_get('jy_daijia_order',array('id'=>$id));
		if($order['status']!=10){
			exi('未支付订单无法完成！','error');
		}
		$set=$this->plugin_setting('setting');
		$bili=$set['credit']?$set['credit']/100:0.2;
		$credit=round($order['price']*$bili,2);
		pdo_update('core_members',array('credit2 -='=>$credit),array('id'=>$order['driver_id']));
		pdo_update('jy_daijia_order',array('status'=>5),array('id'=>$id));
		$driver_id=pdo_getcolumn('jy_daijia_order',array('id'=>$id),'driver_id');
		pdo_update('jy_daijia_driver',array('busy'=>1),array('id'=>$driver_id));
		
	}
	exi('已完成','','jy_daijia/order/list/');
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','list');
	}
	$return['item']=pdo_get('jy_daijia_order',array('id'=>$id));
	$return['item']['driver']=pdo_get('jy_daijia_driver',array('id'=>$return['item']['driver_id']),array('name','mobile','sex','avatar','mid'));
	$return['item']['driver']['avatar']=tomedia($return['item']['driver']['avatar']);
	$return['item']['driver']['ordersum']=pdo_count('jy_daijia_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'driver_id'=>$return['item']['driver_id']),'id');
	$return['item']['createtime']=date('Y-m-d H:i:s',$return['item']['createtime']);
	exi($return);
}
if($_RQ['op']=='sendwxapp_neworder'){
	$id = intval($_RQ['id']);
	$this->model('notice')->notice_create2buyer($id);
	exi('success');	
}
if($_RQ['op']=='pay'){
	$order_id = intval($_RQ['id']);
	$order=pdo_get('jy_daijia_order',array('id'=>$order_id));
	$params=array(
		'id'=>$order_id,
		'title'=>'代驾支付',
		'fee'=>$order['price'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_daijia/order/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_daijia/order/',
		'error_url'=>$error_url?:'jy_daijia/order/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_daijia/order/');
	}
	$order=pdo_get('jy_daijia_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_daijia/order/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_daijia/order/');
	}
	$this->pdo_update('order',array('status'=>10),array('id'=>$order['id']));
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_pay($order['id'],$params);
	exi('提交成功!');
}
?>